﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;


public class LLVMBCGenerator {

    void todo() {

    }

    BinaryReader _input;

    TextWriter _output;

    public void generate(BinaryReader input, TextWriter output) {
        _input = input;
        _output = output;

        process();
    }

    OpCodeType readOpCode() {
        return (OpCodeType)_input.ReadByte();
    }

    enum StackValueType {
        i32
    }

    struct StackValue {
        public StackValueType type;
        public string name;
    }

    static int _tmpVarId;

    StackValue[] _stack = new StackValue[32];

    StackValue[] _locals = new StackValue[4]{
        new StackValue(){type = StackValueType.i32, name = "%loc1"},
        new StackValue(){type = StackValueType.i32, name = "%loc2"},
        new StackValue(){type = StackValueType.i32, name = "%loc3"},
        new StackValue(){type = StackValueType.i32, name = "%loc4"},
    };

    StackValue[] _args = new StackValue[4];

    int _stackTop = -1;

    StackValue pop() {
        return _stack[_stackTop--];
    }

    StackValue top() {
        return _stack[_stackTop];
    }

    void push(StackValue value) {
        if (value.name == null) {
            value.name = "%" + ++_tmpVarId; 
        }
        if (++_stackTop == _stack.Length) {
            Array.Resize(ref _stack, _stack.Length << 1);
        }
        _stack[_stackTop] = value;
    }

    void writeLLVMOpCode(string value) {
        _output.WriteLine(value);
    }

    void writeLLVMOpCode(string value, params string[] args) {
        _output.Write(value);
        int index = 0;
        foreach (string arg in args) {
            if (index++ > 0) {
                _output.Write(", ");
            } else {
                _output.Write(" ");
            }
            _output.Write(arg);
        }
        _output.WriteLine();
    }

    void process() {
        int i = (int)_input.BaseStream.Length;
        while (i-- > 0) {
            OpCodeType type = readOpCode();

            switch (type) {
            }
        }
    }

    public void generate(ModuleBuilder module) {

    }

}